package com.gpscookie.mobile;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;

import de.enough.polish.ui.ImageItem;
import de.enough.polish.ui.StringItem;
import de.enough.polish.util.Locale;

// http://ditu.google.cn/staticmap?size=240x320&maptype=mobile&markers=39.904667,116.408198,midgreen|39.904667,116.408198,midgreen|39.909542,116.411964,midgreen|39.910062,116.417174,midgreen|39.91009,116.416941,midgreen|39.910414,116.413086,midgreen|39.912398,116.413464,midgreen|39.914528,116.402747,midgreen|&key=ABQIAAAA3Jctzi6h9XjhBnp3WNhIJxQnE-HqWLobAj7gM0G2Up4_rw7kTBTHMO63dhhpN2Rp5h6NYa--6XHbWw&sensor=false
public class ImageScreen extends BasicScreen implements DownloadListener
{

	private DownloadManager dlManager = null;
	
	public ImageScreen(String title, GPSCookie globalClass) {
		super(title, globalClass);
		this.dlManager = new DownloadManager();
		this.cmdBack = new Command(Locale.get("BACK"), Command.SCREEN, 1);		
		this.addCommand(this.cmdBack);	
		this.parentScreen = GPSCookie.searchScreen;	
		this.appendAd();
	}

	public void commandAction(Command arg0, Displayable arg1) {
		if (arg0 == cmdBack) {
			this.goBack();
		}
	}
	
	private void cleanScreen() {
		this.deleteAll();
		//#style infoText
		this.labInfo = new StringItem(Locale.get("STATUS"), Locale.get("WAITMSG"));
		this.append(this.labInfo);		
	}
	
	public void download(String url) {
		this.cleanScreen();
		String appended = url;		
		this.dlManager.download(this, appended);
		//System.out.println(url);
	}

	public void downloadCompleted(byte[] data) {		
		try {
			if (data==null || data.length<=0) {
				throw new Exception("Not found.");
			}
			ImageItem original = new ImageItem("", Image.createImage(data, 0x00, data.length), ImageItem.LAYOUT_CENTER, Locale.get("MAP"));
			this.deleteAll();
			this.append(original);
		} catch (Exception e) {
			this.labInfo.setText(e.toString());
			e.printStackTrace();			
		}		
	}
	
	public void downloadError(Exception e) {
		this.labInfo.setText(e.getMessage());	
	}

	public void downloadStatus(int percent) {
		this.labInfo.setText(String.valueOf(percent)+"%");		
	}
}
